package com.block.datapush.service.apipush;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.enums.BaseTypeInfoEnum;
import com.block.common.model.param.PushApiKeyParam;
import com.block.common.service.IMemberCommonService;
import com.block.common.service.LogFactory;
import com.block.datapush.enums.PushStatusEnum;
import com.block.datapush.model.PushQueryParam;
import com.block.datapush.model.dto.NoticeParamDTO;
import com.block.datapush.model.result.ProtocolParamResult;
import com.block.datapush.model.result.PushApiResult;
import com.block.datapush.service.IPushApiService;
import com.block.datapush.service.apipush.utils.NingBoSaiUtil;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.entity.MerchantBaseInfo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 鸿文金服
 */
@Service
public class HongWenJinFuApiService implements IPushApiService {

    private static final Logger log = LogFactory.getPushLog();


    @Autowired
    IMemberCommonService memberCommonService;


    @Override
    public PushApiResult checkUser(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {

        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);

        String mobileMd5 = memberSourceInfo.getMobileMd5();
        String queryUrl = pushApiKeyParam.getCheckUrl();


        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("sourceCode", pushApiKeyParam.getApiCode());
        paramMap.put("accessKey", pushApiKeyParam.getApiKey());
        paramMap.put("phoneMd5", mobileMd5);


        log.info("【鸿文金服】撞库开始【queryUrl】{}【paramMap】{}", queryUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(queryUrl)
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(paramMap))
                .execute().body();
        log.info("【鸿文金服】撞库结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        String code = resultObject.getString("code");
        String msg = resultObject.getString("msg");


        pushApiResult.setCode(code + "");
        pushApiResult.setMessage(msg + "");
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(code), "1") && resultObject.getBoolean("success")) {
            JSONObject data = resultObject.getJSONObject("data");
            if (data != null) {
                boolean phoneMd5Exist = data.getBoolean("phoneMd5Exist");
                if (!phoneMd5Exist) {
                    pushApiResult.setPushSuccess(true);
                }
            }
        }

        return pushApiResult;
    }

    @Override
    public PushApiResult queryApprove(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {
        return null;
    }

    @Override
    public PushApiResult registerUser(String orderNo, String outOrderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) throws Exception {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        pushApiResult.setOrderNo(orderNo);

        String requestUrl = pushApiKeyParam.getRegisterUrl();


        Map<Object, Object> bizMap = new HashMap<>();

        bizMap.put("realName", memberSourceInfo.getRealname());
        bizMap.put("age", memberSourceInfo.getAge());
        bizMap.put("phone", memberSourceInfo.getMobile());
        bizMap.put("sex", memberSourceInfo.getSex() == 1 ? 1 : 0);
        bizMap.put("city", memberSourceInfo.getProvince() + "-" + memberSourceInfo.getCity());
        bizMap.put("profession", NingBoSaiUtil.getprofession(memberSourceInfo.getGongzuo()));//职业
        bizMap.put("sesameScore", NingBoSaiUtil.getZhimaScore(memberSourceInfo.getZhima()));
        bizMap.put("loanMoney", NingBoSaiUtil.getloan(memberSourceInfo.getLoanAmount()));
        bizMap.put("loanExpiresMonth", NingBoSaiUtil.getloanTime(memberSourceInfo.getLoanTime()));
        bizMap.put("loanUse", NingBoSaiUtil.getUse(memberSourceInfo.getYongtu()));
        bizMap.put("gjjScop", NingBoSaiUtil.getGjj(memberSourceInfo.getGjj()));
        bizMap.put("sbScop", NingBoSaiUtil.getSocialInsurance(memberSourceInfo.getShebao()));
        bizMap.put("incomeMonth", NingBoSaiUtil.getshouru(memberSourceInfo.getShouru()));
        bizMap.put("house", NingBoSaiUtil.getHouse(memberSourceInfo.getHouse()));
        bizMap.put("car", NingBoSaiUtil.getCar(memberSourceInfo.getCar()));
        bizMap.put("baodanScop", NingBoSaiUtil.getbaodan(memberSourceInfo.getBaoxian()));
        bizMap.put("isLoans", 0);
        bizMap.put("education", NingBoSaiUtil.getxueli(memberSourceInfo.getXueli()));
        bizMap.put("remarks", "花呗额度：" + BaseTypeInfoEnum.Huabei.getNameByValue(memberSourceInfo.getHuabei()) + "--- 白条额度" + BaseTypeInfoEnum.Baitiao.getNameByValue(memberSourceInfo.getBaitiao()));


        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("sourceCode", pushApiKeyParam.getApiCode());
        paramMap.put("dataStr", JSON.toJSONString(bizMap));
        paramMap.put("accessKey", pushApiKeyParam.getApiKey());


        log.info("【鸿文金服】导单开始【queryUrl】{}【paramMap】{}", requestUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(requestUrl)
                .header("Content-Type", "application/json")
                .body(JSON.toJSONString(paramMap))
                .execute().body();
        log.info("【鸿文金服】导单结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        String code = resultObject.getString("code");
        boolean success = resultObject.getBoolean("success");

        pushApiResult.setCode(code + "|");
        pushApiResult.setMessage(success + "");
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(code), "1") & success) {
            pushApiResult.setPushSuccess(true);
            pushApiResult.setSettleFlag(true);
            pushApiResult.setStatus(PushStatusEnum.SUCCESS.getValue());
            return pushApiResult;
        }
        pushApiResult.setPushSuccess(false);
        pushApiResult.setSettleFlag(false);
        pushApiResult.setStatus(PushStatusEnum.FAILED.getValue());
        return pushApiResult;
    }

    @Override
    public PushApiResult queryResult(PushQueryParam pushQueryParam, PushApiKeyParam pushApiKeyParam, String extJson) {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        return pushApiResult;
    }


    @Override
    public NoticeParamDTO getNoticeResultParam(String infoId, Map<String, String[]> parameterMap, String paramContent) {
        NoticeParamDTO noticeParamDTO = new NoticeParamDTO();
        return noticeParamDTO;
    }

    @Override
    public JSONObject getNoticeResultReturn(Map<String, String[]> parameterMap, String paramContent, JSONObject extObject) {
        JSONObject jsonObject = new JSONObject();
        return jsonObject;
    }

    @Override
    public ProtocolParamResult getProtocolResult(MerchantBaseInfo merchantBaseInfo, MemberSourceInfo memberSourceInfo) {
        ProtocolParamResult paramResult = new ProtocolParamResult();
        return paramResult;
    }


}
